import { _decorator, Component, Node, BoxCollider, ITriggerEvent, ICollisionEvent, CapsuleCollider, Scene, director } from 'cc';
import { Group } from '../Common/Constant';
import { GameManager } from '../Manager/GameManager';
import { EasyController, EasyControllerEvent } from './EasyController';
import { PropController, PropType } from './PropController';
const { ccclass, property } = _decorator;

@ccclass('PlayerColliderController')
export class PlayerColliderController extends Component {

    collider: CapsuleCollider = null!;

    public start() {
        this.collider = this.node.getComponent(CapsuleCollider);
        this.collider.on('onTriggerEnter', this.onTriggerEnter, this);
        this.collider.on('onTriggerStay', this.onTriggerStay, this);
        this.collider.on('onTriggerExit', this.onTriggerExit, this);

        this.collider.on('onCollisionEnter', this.onCollisionEnter, this);
        this.collider.on('onCollisionStay', this.onCollisionStay, this);
        this.collider.on('onCollisionExit', this.onCollisionExit, this);
    }

    private onTriggerEnter(event: ITriggerEvent) {
        switch (event.otherCollider.getGroup()) {
            case Group.Gate:
                console.log("Game Over");
                break;
        }
    }

    private onTriggerStay(event: ITriggerEvent) {
    }

    private onTriggerExit(event: ITriggerEvent) {
    }

    private onCollisionEnter(event: ICollisionEvent) {
        let controller;
        switch (event.otherCollider.getGroup()) {
            case Group.Props:
                controller = event.otherCollider.node.parent.getComponent(PropController);
                if (controller) {
                    GameManager.Instance.AddProp(controller.propType);
                    controller?.Destroy();
                }
        }
    }

    private onCollisionStay(event: ICollisionEvent) {
    }

    private onCollisionExit(event: ICollisionEvent) {
    }

    onDestroy() {
        this.collider.off('onTriggerEnter', this.onTriggerEnter, this);
        this.collider.off('onTriggerStay', this.onTriggerStay, this);
        this.collider.off('onTriggerExit', this.onTriggerExit, this);
        this.collider.off('onCollisionEnter', this.onCollisionEnter, this);
        this.collider.off('onCollisionStay', this.onCollisionStay, this);
        this.collider.off('onCollisionExit', this.onCollisionExit, this);
    }
}


